package com.vcredit.creditcat.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.vcredit.creditcat.model.Feedback;
import com.vcredit.creditcat.vo.ChanneManVo;
import com.vcredit.creditcat.vo.FeedbackVo;
import com.vcredit.creditcat.service.ExportService;
import com.vcredit.creditcat.service.FeedbackService;
import com.vcredit.creditcat.enumtype.ApiClazzEnum;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.web.BaseController;
import com.vcredit.framework.util.JacksonUtil;
 /*
  * @author zhangxingong
  * @version 1.0
  * @since 1.0
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController extends BaseController {
    
  @Autowired
  private FeedbackService feedbackService;
  
  @Autowired
  private  ExportService  exportService;
  
    @RequestMapping("/list")
    public ModelAndView  showPageFeedback(HttpServletRequest request) {
        return new ModelAndView("/feedback/feedbackList");
    }  
	
	@RequestMapping("/listFeedback")
	public Page<Feedback> findByPageFeedback(HttpServletRequest request) {
      return feedbackService.findByPage(makeDefaultCriteria(request));
	}
	
	@RequestMapping("/saveFeedback")
	public String saveFeedback(@RequestBody FeedbackVo feedbackVo,HttpServletRequest request) {
	    feedbackService.save(feedbackVo);
		return SUCCESS;
	}
	   
    @RequestMapping("/updateFeedback")
    public String feedbackUpdate(@RequestBody Feedback feedback,HttpServletRequest request) {
        feedbackService.update(feedback);
        return SUCCESS;
    }
    
    @RequestMapping("/removeFeedback")
    public String removeFeedback(@RequestParam("ids") String ids ,HttpServletRequest request) {
        feedbackService.removeFeedback(ids);
        return SUCCESS;
    }	
    
    @SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/listchanneExport")
	public String listchanneExport(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		String resultMap = "";
		try {
			//获取符合查询条件的数据个数
			Page<Feedback>  page=feedbackService.findByPage(makeDefaultCriteria(request));
			long maxCount=page.getResults();
			//设置分页最大数据为数据个数，再次查询一遍
			Page<Feedback>  page2=feedbackService.findByPage(makeDefaultCriteria2(maxCount,request));
			//导出
			map = exportService.exportExcel(request, response, ApiClazzEnum.feedback.getKey(), page2.getRows());
			resultMap = JacksonUtil.Obj2Json(map);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultMap;
	}
}
